package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.AlbumMapper;
import cn.tedu.csmall.product.mapper.BrandMapper;
import cn.tedu.csmall.product.mapper.CategoryMapper;
import cn.tedu.csmall.product.mapper.SpuMapper;
import cn.tedu.csmall.product.pojo.dto.SpuAddNewDTO;
import cn.tedu.csmall.product.pojo.entity.Spu;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.BrandStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.ISpuService;
import cn.tedu.csmall.product.web.ServiceCode;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SpuServiceImpl implements ISpuService {
    @Autowired
    private SpuMapper spuMapper;
    @Autowired
    private AlbumMapper albumMapper;
    @Autowired
    private BrandMapper brandMapper;
    @Autowired
    private CategoryMapper categoryMapper;

    public SpuServiceImpl() {
        log.debug("创建业务对象：SpuServiceImpl");
    }

    @Override
    public void insert(SpuAddNewDTO spuAddNewDTO) {
        log.debug("开始处理【添加SPU】的业务，参数：{}", spuAddNewDTO);
        AlbumStandardVO albumQueryResult = albumMapper.getStandardById(spuAddNewDTO.getAlbumId());
        if (albumQueryResult==null){
            String message = "添加SPU失败，尝试添加的SPU的相册不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        BrandStandardVO brandQueryResult = brandMapper.getStandardById(spuAddNewDTO.getBrandId());
        if (brandQueryResult==null){
            String message = "添加SPU失败，尝试添加的SPU的品牌不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        if (brandQueryResult.getEnable()==0){
            String message = "添加SPU失败，尝试添加的SPU的品牌已被禁用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        CategoryStandardVO categoryQueryResult = categoryMapper.getStandardById(spuAddNewDTO.getCategoryId());
        if (categoryQueryResult==null){
            String message = "添加SPU失败，尝试添加的SPU的类别不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        if (categoryQueryResult.getEnable()==0){
            String message = "添加SPU失败，尝试添加的SPU的类别已被禁用!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }
        if (categoryQueryResult.getIsParent()==1){
            String message = "添加SPU失败，尝试添加的SPU的类别包含子类别!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        Spu spu = new Spu();
        BeanUtils.copyProperties(spuAddNewDTO, spu);
        log.debug("即将执行插入数据");
        int rows = spuMapper.insert(spu);
        if (rows != 1){
            String message = "添加SPU失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }
}
